﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VK.Comm.TransactSql;
using VK.Comm.Session;
using VK.Comm.Cookie;

namespace VK.Casemeet.Members
{
    public static class MemberBLL
    {
        public static int GetMemberCount(IList<Condition> conditionArray)
        {
            return MemberDAL.GetMemberCount(conditionArray);
        }

        public static List<MemberEntity> GetMemberList(int startRecordIndex, int endRecordIndex, IList<Condition> conditionArray, string sortField, SortMode sortMode)
        {
            return MemberDAL.GetMemberList(startRecordIndex, endRecordIndex, conditionArray, sortField, sortMode);
        }

        public static List<MemberEntity> GetMemberList(IList<Condition> conditionArray, string sortField, SortMode sortMode)
        {
            return MemberDAL.GetMemberList(conditionArray, sortField, sortMode);
        }

        public static MemberEntity GetMember(int memberID)
        {
            return MemberDAL.GetMember(memberID);
        }

        public static int InsertMember(MemberEntity member)
        {
            return MemberDAL.InsertMember(member);
        }

        public static int UpdateMember(MemberEntity member)
        {
            return MemberDAL.UpdateMember(member);
        }

        public static int DeleteMember(int memberID)
        {
            return MemberDAL.DeleteMember(memberID);
        }

        public static int LandingNumber(int memberID)
        {
            return MemberDAL.LandingNumber(memberID);
        }

        public static bool IsSessionMember()
        {
            MemberEntity member = SessionOperate<MemberEntity>.GetEntity(CS.MemberSessionKey);
            if (member == null)
            {
                member = CookieOperate<MemberEntity>.GetEntity(CS.MemberCookieKey);
            }
            if (member != null)
            {
                SessionOperate<MemberEntity>.SetEntity(CS.MemberSessionKey, member);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int Enter(string email, string password, bool isSave)
        {
            int result = 0;
            MemberEntity member = null;
            switch (MemberDAL.Enter(email, password))
            {
                case 1:
                    member = MemberDAL.GetMemberByEmail(email);
                    result = 1;
                    SessionOperate<MemberEntity>.SetEntity(CS.MemberSessionKey, member);
                    if (isSave)
                    {
                        CookieOperate<MemberEntity>.SetEntity(CS.MemberCookieKey, member, 30);
                    }
                    if (member.State == 1)
                    {
                        System.Web.HttpContext.Current.Response.Redirect("/ActiveEmail.html");
                    }
                    break;
                case 2: result = 2; break;
                case 3: result = 3; break;
            }
            return result;
        }

        public static int Register(MemberEntity member)
        {
            int result = 0;

            if (MemberDAL.GetMemberByEmail(member.Email) == null)
            {
                result = MemberDAL.InsertMember(member);
                if (result > 0)
                {
                    MemberEntity tmpmember = MemberDAL.GetMemberByEmail(member.Email);
                    SessionOperate<MemberEntity>.SetEntity(CS.MemberSessionKey, tmpmember);
                    result = 1;
                }
            }
            else
            {
                result = 3;
            }

            return result;
        }

        public static void Leave()
        {
            SessionOperate<MemberEntity>.RemoveEntity(CS.MemberSessionKey);
            CookieOperate<MemberEntity>.RemoveEntity(CS.MemberCookieKey);
        }

        public static MemberEntity GetSessionMember()
        {
            MemberEntity member = SessionOperate<MemberEntity>.GetEntity(CS.MemberSessionKey);
            if (member == null)
            {
                member = CookieOperate<MemberEntity>.GetEntity(CS.MemberCookieKey);
                if (member == null)
                {
                    System.Web.HttpContext.Current.Response.Redirect("/MemberLogin.html");
                }
                else
                {
                    SessionOperate<MemberEntity>.SetEntity(CS.MemberSessionKey, member);
                }
            }
            return member;
        }

        public static int SetMemberPassword(int memberID, string oldPassword, string newPassword)
        {
            return MemberDAL.SetMemberPassword(memberID, oldPassword, newPassword);
        }

        public static int ResetPass(int memberID, string newpass)
        {
            return MemberDAL.ResetPass(memberID, newpass);
        }

        public static int FindPass(string email, string newpass)
        {
            return MemberDAL.FindPass(email, newpass);
        }

        public static MemberEntity GetMemberByEmail(string email)
        {
            return MemberDAL.GetMemberByEmail(email);
        }

        public static MemberEntity GetMemberByValidateCode(string validateCode)
        {
            return MemberDAL.GetMemberByValidateCode(validateCode);
        }

        public static int UpdateMemberValidate(int memberID, string validateCode, DateTime validateDateTime)
        {
            return MemberDAL.UpdateMemberValidate(memberID, validateCode, validateDateTime);
        }
    }
}
